package com.atguigu.jvm.chap05;

import java.util.ArrayList;

/**
 * JDK8使用
 * -Xms30m -Xmx30m -XX:+PrintGCDetails
 * <p>
 * JDK17使用，-XX:+PrintGCDetails参数在JDK17版本中不再被推荐
 * [warning][gc] -XX:+PrintGCDetails is deprecated. Will use -Xlog:gc* instead.
 * -Xms30m -Xmx30m -Xlog:gc*
 * <p>
 * <p>
 * -Xms20m -Xmx20m -Xlog:gc* -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=D:\myDump
 */
public class OOMDemo {
    public static void main(String[] args) throws InterruptedException {
        ArrayList<Dog> list = new ArrayList<>();
        while (true) {

            System.out.print("最大堆大小：Xmx=");
            System.out.println(Runtime.getRuntime().maxMemory() / 1024.0 / 1024 + "M");

            System.out.print("剩余堆大小：free mem=");
            System.out.println(Runtime.getRuntime().freeMemory() / 1024.0 / 1024 + "M");

            System.out.print("当前堆大小：total mem=");
            System.out.println(Runtime.getRuntime().totalMemory() / 1024.0 / 1024 + "M");

            list.add(new Dog());

            Thread.sleep(100);
            System.out.println();

            //System.gc();   // C  C++  java = (C++)--
        }
    }
}

//-Xms30m -Xmx30m -XX:+PrintGCDetails

//-Xms20m -Xmx20m -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=D:\myDump


//Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
class Dog {
    Integer id;

    String dogName;

    byte[] bytes = new byte[2 * 1024 * 1024];//1MB
}